#include "Tools/ToolAddObject.h"

using namespace Ogre;
ToolAddObject::ToolAddObject(Ogre::Camera *mCam, Ogre::SceneManager *mScMgr, std::string oName, RaySceneQuery* ray)
{

    SceneNode *node;
	objectName = oName.c_str();
	mCamera = mCam;
	node = createNode();

	moveObject = new ActionMoveObject(mCam,node,ray);
	moveCam = new ActionMoveCamera(mCam);
	scale = new ActionScaleObject(node);

	altPressed=false;
}

ToolAddObject::~ToolAddObject(){
	//destroy objects and queries
}

void ToolAddObject::updateGraphics(const FrameEvent &evt){

	moveCam->updateGraphics(evt);
	moveObject->updateGraphics(evt);
}

void ToolAddObject::mouseMoved(const OIS::MouseEvent &evt){

	scale->mouseMoved(evt);
	moveObject->mouseMoved(evt);
	moveCam->mouseMoved(evt);
}

void ToolAddObject::mousePressed(const OIS::MouseEvent &evt){
	
	if(!altPressed){
		moveObject->mousePressed(evt);
		MapEditor::instance()->toolEnded();
	}
	else{
		moveObject->mousePressed(evt);
		moveObject->changeObject(createNode());
	}
}

void ToolAddObject::keyPressed(const OIS::KeyEvent &evt){
	
	if(evt.key==OIS::KC_LMENU)
		altPressed=true;
	moveCam->keyPressed(evt);
}
void ToolAddObject::keyReleased(const OIS::KeyEvent &e){
	
	if(e.key==OIS::KC_LMENU)
		altPressed=false;
	moveCam->keyReleased(e);
}

SceneNode* ToolAddObject::createNode(){

	SceneNode *node;

	std::string nodeName="SNode"+Constants::instance()->getNum();
	Entity *ent = mCamera->getSceneManager()->createEntity(nodeName, objectName);

	node = mCamera->getSceneManager()->getRootSceneNode()->createChildSceneNode(nodeName);
	node->attachObject(ent);
	node->showBoundingBox(true);
	return node;

}
